<template>
  <div class="regist">
    <div class="box">
      <p>账户信息</p>
      <div class="content">
        <el-form ref="registForm" :model="registForm" :rules="rules" label-width="100px" inline-message size="mini" >
          <el-form-item prop="userName" label="用户名">
            <el-input v-model="registForm.userName" prefix-icon="el-icon-user" placeholder="用户名设置成功后不可修改" />
          </el-form-item>
          <el-form-item prop="password" label="登陆密码">
            <el-input type="password" v-model="registForm.password" prefix-icon="el-icon-lock" placeholder="6-20位字母、数字或符号" />
          </el-form-item>
          <el-form-item label="确认密码" prop="password">
            <el-input type="password" v-model="registForm.secondPass" prefix-icon="el-icon-lock" placeholder="再次输入您的登陆密码" />
          </el-form-item>
          <el-form-item label="证件类型" prop="cardtype">
            <el-select v-model="registForm.cardtype">
              <el-option label="中国居民身份证" value="1" ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="pname" label="姓名">
            <el-input v-model="registForm.pname"  placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item prop="cardid" label="证件号码">
            <el-input v-model="registForm.cardid"  placeholder="请输入您的证件号码" />
          </el-form-item>
          <el-form-item label="旅客类型" prop="ptype">
            <el-select v-model="registForm.ptype" placeholder="请选择旅客类型">
              <el-option label="成人" value="成人"></el-option>
              <el-option label="儿童" value="儿童"></el-option>
              <el-option label="学生" value="学生"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="email" label="邮箱">
            <el-input v-model="registForm.email" placeholder="请正确填写邮箱地址" >
              <template slot="suffix">
                <el-button @click="sendMail" type="primary" size="mini">发送邮件</el-button>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="code" label="验证码">
            <el-input v-model="registForm.code"  placeholder="请输入您收到的验证码" />
          </el-form-item>
          <el-form-item prop="phone" label="手机号码">
            <el-input v-model="registForm.phone"  placeholder="请输入您的手机号码" />
          </el-form-item>
          <el-form-item  prop="checked">
            <el-checkbox v-model="registForm.checked">我已阅读并同意遵守 《中国铁路客户服务中心网站服务条款》 《隐私权政策》</el-checkbox>
          </el-form-item>
          <el-form-item style="margin-top:20px;">
            <el-button style="width:100px;" type="warning" @click="onSubmit">注 册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios';
  export default {
    data(){
      var boolean = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请勾选协议'));
        } else {
          callback();
        }
      };
      return {
        registForm:{
          userName:'',
          password:'',
          pname:'',
          cardid:'',
          ptype:'',
          email:'',
          code:'',
          phone:'',
          cardtype:'1',
          secondPass:'',
          checked:false,
        },
        rules:{
          userName: [
            { required: true, message: "请输入用户名/邮箱/手机号", trigger: "blur" }
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" }
          ],
          email: [
            { required: true, message: "请填写正确的邮箱，稍后将发送验证码", trigger: "blur" }
          ],
          pname: [
            { required: true, message: "用于身份验证，请正确填写", trigger: "blur" }
          ],
          cardid: [
            { required: true, message: "用于身份核验，请正确填写", trigger: "blur" }
          ],
          ptype: [
            { required: true, trigger: "blur" }
          ],
          code: [
            { required: true,  trigger: "blur" }
          ],
          phone: [
            { required: true,  trigger: "blur" }
          ],
          cardtype: [
            { required: true,  trigger: "blur" }
          ],
          checked: [
            { validator: boolean,required: true, message: "请勾选协议", trigger: "blur" }
          ],
        },
        open:false
      }
    },
    methods:{
      sendMail:function(){
        console.log(this.registForm.email)
        axios.post('/api/sendSMail?email='+this.registForm.email).then(res=>{
          if(res.data.code == 0){
            alert("验证码已发送，五分钟内有效");
          }
        })
      },


      /** 提交按钮 */
      onSubmit:function() {
        console.log(this.registForm);
        this.$refs["registForm"].validate(valid => {
            if (valid) {
                // 此处调取接口
                axios.post('/api/user/sigin',this.registForm).then(res=>{
                  console.log(res.data)
                  if(res.data.code == 0){
                    //注册成功，跳转登陆页面
                    this.$router.push("/login");
                  }else{
                    alert(res.data.msg);
                  }
                })
            }
        });
        
      },
    }
  }

</script>

<style scoped>
.regist{
  display: flex;
  justify-content: center;
}
.box{
  width: 70%;
  margin: 30px 0;
  display: flex;
  flex-direction: column;
  /* justify-content: center; */
  align-items: center;
  border: 1px solid #1678be;
  border-radius: 5px 5px 0 0;
  padding-bottom: 20px;
}
.box p{
  /* justify-content: ; */
  place-self: flex-start;
  margin: 0;
  width: 98%;
  color: white;
  padding: 10px 0;
  background: #2676E3;
  margin-bottom: 20px;
  padding-left: 2%;
  border-radius: 5px 5px 0 0;
}
  .regist .content{
    width: 350px;
    /* align:center; */
    background: white;

  }
  .content /deep/ .el-form-item{
    margin-bottom: 0px;
  }
</style>
